package top.vanchor.flexible.common;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 响应数据
 * @author hw
 */
@Getter
@Setter
@ToString
public class ResponseData<T> {
    private Integer code;
    private String message;
    private T data;

    public static <T> ResponseData<T> ok(T data){
        ResponseData<T> res = new ResponseData<>();
        res.setData(data);
        res.setCode(ResponseCode.SUCCESS.getCode());
        res.setMessage(ResponseCode.SUCCESS.getMsg());
        return res;
    }

    public static <T> ResponseData<T> warn(String message){
        ResponseData<T> res = new ResponseData<>();
        res.setData(null);
        res.setCode(ResponseCode.WARN.getCode());
        res.setMessage(message);
        return res;
    }

    public static <T> ResponseData<T> serverFail(){
        return new ResponseData<>(ResponseCode.SERVER_ERR);
    }

    public static class Builder<T>{
        private int code;
        private String message;
        private T data;

        public Builder<T> code(int code) {
            this.code = code;
            return this;
        }

        public Builder<T> message(String message){
            this.message = message;
            return this;
        }

        public Builder<T> data(T data){
            this.data = data;
            return this;
        }

        public ResponseData<T> build(){
            return new ResponseData<>(this);
        }
    }

    public ResponseData(){

    }

    private ResponseData(Builder<T> builder){
        this.code = builder.code;
        this.message = builder.message;
        this.data = builder.data;
    }

    public ResponseData(ResponseCode responseCode){
        this.data = null;
        this.code = responseCode.getCode();
        this.message = responseCode.getMsg();
    }

}
